
package common.project.epic.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import common.project.epic.core.EpicSynch;
import common.project.epic.core.IEpicSynch;
import common.project.epic.db.CurrentUser;

public class EpicProfile extends Activity {
    
	private ImageView loggedUserPhoto;
	private ImageButton synch_button;
	
	private TextView profileInfoText_1;
	private TextView profileInfoText_2;
	private TextView profileInfoText_3;
	private TextView profileInfoText_4;
	private static final String[] CATEGORIES = {"General","Leisure","Advertisements","Suggestions",
												"Events","","","",
												"","","","",
												"","","",""};
	
	private IEpicSynch mISynchEpic;
	
	private ProgressDialog dialog;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.epicprofile);
        
        CurrentUser.readCurrentUserSettings(getApplicationContext());
        
        loggedUserPhoto = (ImageView) findViewById(R.id.loggedUserPhoto_IV);
        synch_button = (ImageButton) findViewById(R.id.synch_button);
        
        profileInfoText_1 = (TextView)findViewById(R.id.profileInfoText_1);
        profileInfoText_1.setText("" + CurrentUser.getName() + " (" + CurrentUser.getCurrentNickname() + ")");
        profileInfoText_2 = (TextView)findViewById(R.id.profileInfoText_2);
        profileInfoText_2.setText("" + CurrentUser.getCurrentUsername());
        profileInfoText_3 = (TextView)findViewById(R.id.profileInfoText_3);
        profileInfoText_3.setText("" + CurrentUser.getDefaultMsgPlace());
        profileInfoText_4 = (TextView)findViewById(R.id.profileInfoText_4);
        String tmp = "";
        String cat = CurrentUser.getPrefCategories();
        for(int i=0; i<16; i++) {
        	if(cat.charAt(i)=='1') {
        		if(tmp.equals("")) {}
        		else {tmp = tmp + ", ";}
        		tmp = tmp + CATEGORIES[i];
        	}
        }
        profileInfoText_4.setText("" + tmp);
        
        synch_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent r = new Intent(getApplicationContext(),EpicSynch.class);
	            startService(r);
//	            dialog = ProgressDialog.show(getApplicationContext(), "", "Synchronizing. Please wait...", false);
//	            dialog.show();
			}
		});
    }
    
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className,IBinder service) {
        	 mISynchEpic = IEpicSynch.Stub.asInterface(service);
        }

        public void onServiceDisconnected(ComponentName className) {
        	mISynchEpic = null;
        }
    };
    
//    /** Nested class that performs progress calculations (counting) */
//    private class ProgressThread extends Thread {
//            Handler mHandler;
//            final static int STATE_DONE = 0;
//            final static int STATE_RUNNING = 1;
//            int mState;
//           
//            ProgressThread(Handler h) {
//                mHandler = h;
//            }
//           
//            public void run() {
//                mState = STATE_RUNNING;   
//                while (mState == STATE_RUNNING) {
//                    try {
//                        Thread.sleep(100);
//                    } catch (InterruptedException e) {
//                        Log.e("ERROR", "Thread Interrupted");
//                    }
//                    Message msg = mHandler.obtainMessage();
//                    if(mISynchEpic!= null){
//	                    try {
//							msg.arg1 = mISynchEpic.getPercentage();
//						} catch (RemoteException e) {
//							e.printStackTrace();
//						}
//                    }
//                    mHandler.sendMessage(msg);
//                    
//                }
//            }
//            
//            /* sets the current state for the thread,
//             * used to stop the thread */
//            public void setState(int state) {
//                mState = state;
//            }
//        }
    
}